{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第四次练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 请务必交到exer4文件夹下，谢绝交到master下\n",
    "+ 请不要改动任何文件，拜托\n",
    "+ 请在10月30日前提交。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请写一下姓名和学号：\n",
    "+ 姓名  \n",
    "+ 学号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "姓名：岑雪莹\n",
    "学号：0165201"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目一\n",
    "一般的歌，分为主歌和副歌部分。副歌有重复，对比两大功能。这就是A－B的基本曲式形式结构。  \n",
    "请找一首你的主打歌，把歌词打印出来，重复的副歌部分，请写一个函数，调用函数打印出来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "副歌:\n",
      "话总说不清楚\n",
      "该怎么明了\n",
      "一字一句像 圈套\n",
      "旧帐总翻不完 谁无理取闹\n",
      "你的双手甩开刚好的微妙\n",
      "然后战火再燃烧\n",
      "主歌:\n",
      "我们背对背拥抱\n",
      "滥用沉默在咆哮\n",
      "爱情来不及变老\n",
      "葬送在烽火的玩笑\n",
      "我们背对背拥抱\n",
      "真话兜着圈子乱乱绕\n",
      "只是想让我知道\n",
      "只是想让你知道\n",
      "爱的警告\n",
      "副歌:\n",
      "话总说不清楚\n",
      "该怎么明了\n",
      "一字一句像 圈套\n",
      "旧帐总翻不完 谁无理取闹\n",
      "你的双手甩开刚好的微妙\n",
      "然后战火再燃烧\n",
      "主歌:\n"
     ]
    }
   ],
   "source": [
    "def song_lyrics(refrain=None,verse=None):\n",
    "    print(\"副歌:%s\" % refrain)\n",
    "    print(\"主歌:%s\" % verse)\n",
    "\n",
    "\n",
    "song_lyrics('\\n话总说不清楚\\n该怎么明了\\n一字一句像 圈套\\n旧帐总翻不完 谁无理取闹\\n你的双手甩开刚好的微妙\\n然后战火再燃烧','\\n我们背对背拥抱\\n滥用沉默在咆哮\\n爱情来不及变老\\n葬送在烽火的玩笑\\n我们背对背拥抱\\n真话兜着圈子乱乱绕\\n只是想让我知道\\n只是想让你知道\\n爱的警告')    \n",
    "song_lyrics('\\n话总说不清楚\\n该怎么明了\\n一字一句像 圈套\\n旧帐总翻不完 谁无理取闹\\n你的双手甩开刚好的微妙\\n然后战火再燃烧','')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目二\n",
    "请写一个函数，能够计算均值、样本方差和n(自己写公式哦)。\n",
    "每次输入十个以上的数字，能够输出上述三个统计量。\n",
    "至少调用两次。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "There are 10 numbers\n",
      "The average = 5.50\n",
      "The varience is 8.25\n",
      "\n",
      "There are 10 numbers\n",
      "The average = 25.50\n",
      "The varience is 767.05\n"
     ]
    }
   ],
   "source": [
    "def function(*numbers):\n",
    "    average=0\n",
    "    var=0\n",
    "    sumvar=0\n",
    "    n=0\n",
    "    sum=0\n",
    "    for value in numbers:\n",
    "        sum=sum+value\n",
    "        n+=1\n",
    "    average=sum/n\n",
    "    print(\"\\nThere are %s numbers\"%n)\n",
    "    print(\"The average = %.2f\"%(average))\n",
    "    for value in numbers:\n",
    "        sumvar=sumvar+(value-average)**2\n",
    "    var=sumvar/n\n",
    "    print(\"The varience is %.2f\"%var)\n",
    "\n",
    "function(1,2,3,4,5,6,7,8,9,10)\n",
    "function(23,5,90,15,17,6,4,8,67,20)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目三\n",
    "刚才那个函数，默认的是输出均值、样本方差和n，请在后面添加一个关键词参数，让这个函数可以选择输出样本标准差或方差。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "There are 10 numbers\n",
      "The average = 5.50\n",
      "输出样本标准差请按1，方差请按2\n",
      "1\n",
      "样本标准差为2.87\n",
      "\n",
      "\n",
      "There are 10 numbers\n",
      "The average = 25.50\n",
      "输出样本标准差请按1，方差请按2\n",
      "2\n",
      "样本方差为767.05\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def function(*numbers):\n",
    "    average=0\n",
    "    var=0\n",
    "    sumvar=0\n",
    "    n=0\n",
    "    sum=0\n",
    "    for value in numbers:\n",
    "        sum=sum+value\n",
    "        n+=1\n",
    "    average=sum/n\n",
    "    print(\"\\nThere are %s numbers\"%n)\n",
    "    print(\"The average = %.2f\"%(average))\n",
    "    for value in numbers:\n",
    "        sumvar=sumvar+(value-average)**2\n",
    "    var=sumvar/n\n",
    "    standard=var**(1/2)\n",
    "    print(\"输出样本标准差请按1，方差请按2\")\n",
    "    choice=int(input())\n",
    "    if choice==1:\n",
    "        print(\"样本标准差为%.2f\\n\"%standard)\n",
    "    if choice==2:\n",
    "        print(\"样本方差为%.2f\\n\"%var)\n",
    "    \n",
    "function(1,2,3,4,5,6,7,8,9,10)\n",
    "function(23,5,90,15,17,6,4,8,67,20)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目四\n",
    "刚才那个函数，请再增加三个关键词参数，让这个参数可以选择输出均值的标准误、最大值、最小值或不输出这三个统计量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "There are 10 numbers\n",
      "The average = 5.50\n",
      "输出样本标准差请按1，方差请按2\n",
      "1\n",
      "样本标准差为2.87\n",
      "\n",
      "输出均值的标准误请按1，输出最大值请按2，输出最小值请按3，按0不输出\n",
      "1\n",
      "均值的标准误为0.91\n",
      "\n",
      "\n",
      "There are 10 numbers\n",
      "The average = 25.50\n",
      "输出样本标准差请按1，方差请按2\n",
      "1\n",
      "样本标准差为27.70\n",
      "\n",
      "输出均值的标准误请按1，输出最大值请按2，输出最小值请按3，按0不输出\n",
      "0\n",
      "None\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def function(*numbers):\n",
    "    average=0\n",
    "    var=0\n",
    "    sumvar=0\n",
    "    n=0\n",
    "    sum=0\n",
    "    for value in numbers:\n",
    "        sum=sum+value\n",
    "        n+=1\n",
    "    average=sum/n\n",
    "    print(\"\\nThere are %s numbers\"%n)\n",
    "    print(\"The average = %.2f\"%(average))\n",
    "    for value in numbers:\n",
    "        sumvar=sumvar+(value-average)**2\n",
    "    var=sumvar/n\n",
    "    standard=var**(1/2)\n",
    "    standard_error=standard/(n**(1/2))\n",
    "    print(\"输出样本标准差请按1，方差请按2\")\n",
    "    choice=int(input())\n",
    "    if choice==1:\n",
    "        print(\"样本标准差为%.2f\\n\"%standard)\n",
    "    if choice==2:\n",
    "        print(\"样本方差为%.2f\\n\"%var)\n",
    "        \n",
    "    print(\"输出均值的标准误请按1，输出最大值请按2，输出最小值请按3，按0不输出\")\n",
    "    choice=int(input())\n",
    "    if choice==1:\n",
    "        print('均值的标准误为%.2f\\n'%standard_error)\n",
    "    if choice==2:\n",
    "        print('均值的最大值为%.2f\\n'%max(numbers))\n",
    "    if choice==3:\n",
    "        print('均值的最小值为%.2f\\n'%min(numbers))\n",
    "    if choice==0:\n",
    "        print(\"None\\n\")\n",
    "    \n",
    "function(1,2,3,4,5,6,7,8,9,10)\n",
    "function(23,5,90,15,17,6,4,8,67,20)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
